package com.anlogic.sdk.templates.core.processes;

import org.eclipse.cdt.core.templateengine.TemplateCore;
import org.eclipse.cdt.core.templateengine.process.ProcessArgument;
import org.eclipse.cdt.core.templateengine.process.ProcessFailureException;
import org.eclipse.cdt.core.templateengine.process.ProcessRunner;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;

import com.anlogic.sdk.openocd.OpenocdLaunchConfiguration;
import com.anlogic.sdk.utils.toolchain.ToolchainUtils;

public class SetLuanchConfigurations extends ProcessRunner  {
	public void process(TemplateCore template, ProcessArgument[] args, String processId, IProgressMonitor monitor) throws ProcessFailureException {
		String projectName = args[0].getSimpleValue();
		String os = args[1].getSimpleValue();
		String arch = args[2].getSimpleValue();
		String projectType = args[3].getSimpleValue();
		String chipName = args[4].getSimpleValue();
		String procName = args[5].getSimpleValue();
		
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		
		OpenocdLaunchConfiguration launchConfig = OpenocdLaunchConfiguration.newInstance(project.getName());
		launchConfig.setRunPlatformInit(ToolchainUtils.getRunPlatInit(chipName));
		launchConfig.setGdbServerExecutable(ToolchainUtils.getOpenOCDExecutable());
		launchConfig.setGdbServerOtherOptions(ToolchainUtils.getGdbServerOtherOptions(arch, projectType));
		launchConfig.setGdbClientExecutable(ToolchainUtils.getGDBExecutable(os, arch, projectType));
		launchConfig.setGdbClientOtherCommands(ToolchainUtils.getGdbClientCommands(arch, projectType));
		launchConfig.setGdbServerPort(Integer.parseInt(ToolchainUtils.getGdbServerPort(chipName, procName)));
		launchConfig.setRemotePort(Integer.parseInt(ToolchainUtils.getGdbServerPort(chipName, procName)));
		launchConfig.setFirstResetType("init");
		launchConfig.setSecondResetType("halt");
		launchConfig.doSave();
	}
}
